package Algorithm.StackAndQueue;

import java.util.Stack;

public class TwoStacksQueue {
    public Stack<Integer> stackPush;
    public Stack<Integer> stackPop;

    public TwoStacksQueue() {
        this.stackPush = new Stack<>();
        this.stackPop = new Stack<>();
    }

    // push栈向pop栈倒数据
    public void pushToPop() {
        if (this.stackPop.isEmpty()) {
            while (!this.stackPush.isEmpty()) {
                this.stackPop.push(this.stackPush.pop());
            }
        }
    }

    public void add(int pushInt) {
        this.stackPush.push(pushInt);
        pushToPop();
    }

    public int poll() {
        pushToPop();
        if (this.stackPop.isEmpty()) {
            throw new RuntimeException("Queue is empty.");
        }
        return this.stackPop.pop();
    }

    public int peek() {
        pushToPop();
        if (this.stackPop.isEmpty()) {
            throw new RuntimeException("Queue is empty.");
        }
        return this.stackPop.peek();
    }
}
